package com.weiyi.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/index.do")
public class CookiesDemo extends HttpServlet{
	protected void processRequest(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		Cookie[] cookies = req.getCookies();	
		if(cookies != null) {
			for(Cookie cookie:cookies) {
				String name = cookie.getName();
				String value = cookie.getValue();
				if("username".equals(name) && "weiyi2020".equals(value)) {
					req.setAttribute(name, value);
					req.getRequestDispatcher("/user.view").forward(req, resp);
					return;
				}
			}
		}
		resp.sendRedirect("login.html");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		processRequest(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		processRequest(req,resp);
	}
}



